package com.sunyur.tailor.channel.service.inner.recycle.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurListResult;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelListResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelListResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.recycle.model.RecycleHeadModel;
import com.sunyur.tailor.channel.service.inner.recycle.model.RecycleListItemModel;
import com.sunyur.tailor.channel.service.inner.recycle.param.RecycleAuditParam;
import com.sunyur.tailor.channel.service.inner.recycle.param.RecycleConfirmParam;
import com.sunyur.tailor.channel.service.inner.recycle.param.RecycleHeadParam;
import com.sunyur.tailor.channel.service.inner.recycle.param.RecycleRollBackStatusParam;
import com.sunyur.tailor.channel.service.inner.recycle.service.RecycleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author: HuBaoding
 * @Description:
 * @Date: Created in 14:40 2021/5/14
 */
@Service("recycleService")
public class RecycleServiceImpl implements RecycleService {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(RecycleServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(RecycleServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    @Value("${sunyur.api.recycleHead}")
    private String recycleHead;

    @Value("${sunyur.api.recycleListItem}")
    private String recycleListItem;

    @Value("${sunyur.api.recycleOutsideAudit}")
    private String recycleOutsideAudit;

    @Value("${sunyur.api.recycleConfirm}")
    private String recycleConfirm;

    @Value("${sunyur.api.recycleRollBackStatus}")
    private String recycleRollBackStatus;

    @Override
    public ChannelResult<RecycleHeadModel> getRecycleHead(RecycleHeadParam param) {
        SunyurResult<RecycleHeadModel> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(recycleHead, JSON.toJSONString(param), new TypeReference<SunyurResult<RecycleHeadModel>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecycleServiceImpl getRecycleHead fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecycleServiceImpl getRecycleHead param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 根据单号获取利旧行信息
     * @param param
     * @return
     */
    @Override
    public ChannelListResult<RecycleListItemModel> getRecycleItemList(RecycleHeadParam param) {
        SunyurListResult<RecycleListItemModel> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execListPost(recycleListItem, JSON.toJSONString(param), new TypeReference<SunyurListResult<RecycleListItemModel>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecycleServiceImpl getRecycleItemList fail,param is {} ",JSON.toJSONString(param));
            return ChannelListResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecycleServiceImpl getRecycleItemList param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelListResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 利旧单外部审批
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> recycleAudit(RecycleAuditParam param) {
        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(recycleOutsideAudit, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecycleServiceImpl recycleAudit fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecycleServiceImpl recycleAudit param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 利旧领用,确认领用
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> recycleConfirm(RecycleConfirmParam param) {
        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(recycleConfirm, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecycleServiceImpl recycleConfirm fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecycleServiceImpl recycleConfirm param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 利旧单状态回滚
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> recycleRollBackStatus(RecycleRollBackStatusParam param) {
        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(recycleRollBackStatus, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("RecycleServiceImpl recycleRollBackStatus fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("RecycleServiceImpl recycleRollBackStatus param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }
}
